package kotan.view.table;

import java.awt.Component;

import javax.swing.AbstractListModel;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.JTable;
import javax.swing.UIManager;


/**
 * Table header for Entity table.
 * @author shuji.w6e
 * @since 1.0
 */
class EntityTableHeader extends JList {

    private static final long serialVersionUID = 1L;
    private final JTable table;

    EntityTableHeader(JTable table) {
        this.table = table;
        setAutoscrolls(false);
        setCellRenderer(new RowHeaderRenderer());
        setFixedCellHeight(table.getRowHeight());
        setFixedCellWidth(20);
        setFocusable(false);
        setModel(new ListModel());
        setOpaque(true);
        setSelectionModel(table.getSelectionModel());
    }

    class ListModel extends AbstractListModel {

        private static final long serialVersionUID = 1L;

        @Override
        public int getSize() {
            return table.getRowCount();
        }

        @Override
        public Object getElementAt(int index) {
            return ((EntityTableModel) table.getModel()).get(index);
        }
    }

    class RowHeaderRenderer extends DefaultListCellRenderer {
        private static final long serialVersionUID = 1L;

        RowHeaderRenderer() {
            setHorizontalAlignment(CENTER);
            setOpaque(true);
            setBorder(UIManager.getBorder("TableHeader.cellBorder"));
            setFont(table.getTableHeader().getFont());
            setBackground(table.getTableHeader().getBackground());
            setForeground(table.getTableHeader().getForeground());
        }

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                boolean cellHasFocus) {
            if (value == null) {
                setText("*");
            } else if (isSelected) {
                setText(">");
            } else {
                setText("");
            }
            return this;
        }
    }
}
